package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolFinancialIndicator;
import cn.da.shuai.cool.finance.model.vo.SearchFinancialIndicatorVo;
import cn.da.shuai.cool.finance.service.ICoolFinancialIndicatorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 财务指标表 前端控制器
 * </p>
 *
 * @author 大帅哥
 * @since 2023-05-01
 */
@Tag(name = "财务指标管理")
@RestController
@RequestMapping("/finance/financial-indicator")
@RequiredArgsConstructor
public class CoolFinancialIndicatorController {

    private final ICoolFinancialIndicatorService financialIndicatorService;

    @Operation(summary = "分页查询财务指标")
    @PostMapping("/page")
    public R<Page<CoolFinancialIndicator>> page(@RequestBody SearchFinancialIndicatorVo vo) {
        LambdaQueryWrapper<CoolFinancialIndicator> wrapper = new LambdaQueryWrapper<CoolFinancialIndicator>()
                .eq(vo.getIndicatorName() != null, CoolFinancialIndicator::getIndicatorName, vo.getIndicatorName())
                .eq(vo.getCalculationPeriod() != null, CoolFinancialIndicator::getCalculationPeriod, vo.getCalculationPeriod())
                .eq(vo.getIndicatorType() != null, CoolFinancialIndicator::getIndicatorType, vo.getIndicatorType())
                .orderByDesc(CoolFinancialIndicator::getCalculationPeriod);

        return R.ok(financialIndicatorService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper));
    }

    @Operation(summary = "获取财务指标详情")
    @GetMapping("/{id}")
    public R<CoolFinancialIndicator> getById(@PathVariable Long id) {
        return R.ok(financialIndicatorService.getById(id));
    }

    @Operation(summary = "新增财务指标")
    @PostMapping
    public R<Boolean> add(@RequestBody CoolFinancialIndicator financialIndicator) {
        return R.ok(financialIndicatorService.save(financialIndicator));
    }

    @Operation(summary = "修改财务指标")
    @PutMapping
    public R<Boolean> update(@RequestBody CoolFinancialIndicator financialIndicator) {
        return R.ok(financialIndicatorService.updateById(financialIndicator));
    }

    @Operation(summary = "删除财务指标")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(financialIndicatorService.removeById(id));
    }
}